import React from 'react'
import { Table, Tag } from 'antd'
import _ from 'lodash'
import { FormattedMessage } from 'react-intl'

class TaskAssignmentsTag extends React.Component {

  getAssignmentsTitle = taskAssignmentsTag => {
    return Object.keys(_.groupBy(taskAssignmentsTag, item => item.assignmentTitle))
  }

  formatTags = (item) => {
    if (!item) return ''

    const tagColors = item.split(',')

    return tagColors.map((item, index) => {
      const tagColor = item.split('|')
      return <Tag color={tagColor[1]} key={index}>{tagColor[0]}</Tag>
    })
  }
  getColumns = taskAssignmentsTag => {
    let titles = this.getAssignmentsTitle(taskAssignmentsTag)
    const columns = [{
      title: <FormattedMessage id='姓名'/>,
      dataIndex: 'userName',
      width: 80
    }]
    titles.forEach(item => columns.push({
      title: item,
      dataIndex: item,
      width: 100,
      render: (text) => {
        return <span>{this.formatTags(text)}</span>
      }
    }))
    return columns
  }

  getData = taskAssignmentsTag => {
    let result = []
    let groupByUserNameAssignmentsTag = _.groupBy(taskAssignmentsTag, item => item.userName)
    Object.keys(groupByUserNameAssignmentsTag).forEach(userName => {
      let temp = {userName}
      let groupByAssignmentTitle = _.groupBy(groupByUserNameAssignmentsTag[userName], item => item.assignmentTitle)
      Object.keys(groupByAssignmentTitle).forEach(assignmentTitle => {
        const assTag = groupByAssignmentTitle[assignmentTitle].filter(item => !!item.assTagCount)
          .map(item => item.assTag)
        temp[assignmentTitle] = assTag.join(',')
      })
      result.push(temp)
    })

    return result
  }

  render () {
    const {taskAssignmentsTag} = this.props
    const columns = this.getColumns(taskAssignmentsTag)
    const data = this.getData(taskAssignmentsTag)
    console.log(data)
    return <Table columns={columns}
                  dataSource={data}
                  pagination={false}
                  bordered
                  size='small'/>
  }
}

export default TaskAssignmentsTag
